<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="省份名称" prop="cname">
        <el-input
          v-model="queryParams.cname"
          placeholder="请输入省份名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="省份介绍" prop="cityBrief">
        <el-input
          v-model="queryParams.cityBrief"
          placeholder="请输入省份介绍"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="省份头像" prop="cityAvatar">
        <el-input
          v-model="queryParams.cityAvatar"
          placeholder="请输入省份头像"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="城市等级" prop="cityLevel">
        <el-input
          v-model="queryParams.cityLevel"
          placeholder="请输入城市等级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="省份展示标志" prop="storeFlag">
        <el-select v-model="queryParams.storeFlag" placeholder="请选择省份展示标志" clearable>
          <el-option
            v-for="dict in dict.type.sys_common_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="热度排序" prop="hotSort">
        <el-input
          v-model="queryParams.hotSort"
          placeholder="请输入热度排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销量排序" prop="saleSort">
        <el-input
          v-model="queryParams.saleSort"
          placeholder="请输入销量排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品排序" prop="productSort">
        <el-input
          v-model="queryParams.productSort"
          placeholder="请输入商品排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="城市备用字段1" prop="cityFiled1">
        <el-input
          v-model="queryParams.cityFiled1"
          placeholder="请输入城市备用字段1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="城市备用字段2" prop="cityFiled2">
        <el-input
          v-model="queryParams.cityFiled2"
          placeholder="请输入城市备用字段2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="城市备用字段3" prop="cityFiled3">
        <el-input
          v-model="queryParams.cityFiled3"
          placeholder="请输入城市备用字段3"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="城市备用字段4" prop="cityFiled4">
        <el-input
          v-model="queryParams.cityFiled4"
          placeholder="请输入城市备用字段4"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="crTime">
        <el-date-picker clearable
          v-model="queryParams.crTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="upTime">
        <el-date-picker clearable
          v-model="queryParams.upTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['store:province:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['store:province:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['store:province:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['store:province:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="provinceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户ID" align="center" prop="provinceId" />
      <el-table-column label="省份名称" align="center" prop="cname" />
      <el-table-column label="省份介绍" align="center" width="350" prop="cityBrief" />
      <el-table-column label="省份头像" align="center" width="180" prop="cityAvatar" >
      <template slot-scope="scope">
          <el-image
      style="width: 100px; height: 100px"
      :src="scope.row.cityAvatar"
      :fit="fit"></el-image>
        </template>
        </el-table-column>
      <el-table-column label="城市等级" align="center" prop="cityLevel" />
      <el-table-column label="省份展示标志" align="center" prop="storeFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_common_status" :value="scope.row.storeFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="热度排序" align="center" prop="hotSort" />
      <el-table-column label="销量排序" align="center" prop="saleSort" />
      <el-table-column label="商品排序" align="center" prop="productSort" />
      <el-table-column label="城市备用字段1" align="center" prop="cityFiled1" />
      <el-table-column label="城市备用字段2" align="center" prop="cityFiled2" />
      <el-table-column label="城市备用字段3" align="center" prop="cityFiled3" />
      <el-table-column label="城市备用字段4" align="center" prop="cityFiled4" />
      <el-table-column label="创建时间" align="center" prop="crTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.crTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="upTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.upTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['store:province:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['store:province:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改热门省份对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="省份名称" prop="cname">
          <el-input v-model="form.cname" placeholder="请输入省份名称" />
        </el-form-item>
        <el-form-item label="省份介绍" prop="cityBrief">
          <el-input v-model="form.cityBrief" placeholder="请输入省份介绍" />
        </el-form-item>
        <el-form-item label="省份头像" prop="cityAvatar">
          <el-input v-model="form.cityAvatar" placeholder="请输入省份头像" />
        </el-form-item>
        <el-form-item label="城市等级" prop="cityLevel">
          <el-input v-model="form.cityLevel" placeholder="请输入城市等级" />
        </el-form-item>
        <el-form-item label="省份展示标志" prop="storeFlag">
          <el-select v-model="form.storeFlag" placeholder="请选择省份展示标志">
            <el-option
              v-for="dict in dict.type.sys_common_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="热度排序" prop="hotSort">
          <el-input v-model="form.hotSort" placeholder="请输入热度排序" />
        </el-form-item>
        <el-form-item label="销量排序" prop="saleSort">
          <el-input v-model="form.saleSort" placeholder="请输入销量排序" />
        </el-form-item>
        <el-form-item label="商品排序" prop="productSort">
          <el-input v-model="form.productSort" placeholder="请输入商品排序" />
        </el-form-item>
        <el-form-item label="城市备用字段1" prop="cityFiled1">
          <el-input v-model="form.cityFiled1" placeholder="请输入城市备用字段1" />
        </el-form-item>
        <el-form-item label="城市备用字段2" prop="cityFiled2">
          <el-input v-model="form.cityFiled2" placeholder="请输入城市备用字段2" />
        </el-form-item>
        <el-form-item label="城市备用字段3" prop="cityFiled3">
          <el-input v-model="form.cityFiled3" placeholder="请输入城市备用字段3" />
        </el-form-item>
        <el-form-item label="城市备用字段4" prop="cityFiled4">
          <el-input v-model="form.cityFiled4" placeholder="请输入城市备用字段4" />
        </el-form-item>
        <el-form-item label="逻辑删除" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入逻辑删除" />
        </el-form-item>
        <el-form-item label="创建时间" prop="crTime">
          <el-date-picker clearable
            v-model="form.crTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="upTime">
          <el-date-picker clearable
            v-model="form.upTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProvince, getProvince, delProvince, addProvince, updateProvince } from "@/api/store/province";

export default {
  name: "Province",
  dicts: ['sys_common_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 热门省份表格数据
      provinceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cname: null,
        cityBrief: null,
        cityAvatar: null,
        cityLevel: null,
        storeFlag: null,
        hotSort: null,
        saleSort: null,
        productSort: null,
        cityFiled1: null,
        cityFiled2: null,
        cityFiled3: null,
        cityFiled4: null,
        crTime: null,
        upTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cityLevel: [
          { required: true, message: "城市等级不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询热门省份列表 */
    getList() {
      this.loading = true;
      listProvince(this.queryParams).then(response => {
        this.provinceList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        provinceId: null,
        cname: null,
        cityBrief: null,
        cityAvatar: null,
        cityLevel: null,
        storeFlag: null,
        hotSort: null,
        saleSort: null,
        productSort: null,
        cityFiled1: null,
        cityFiled2: null,
        cityFiled3: null,
        cityFiled4: null,
        delFlag: null,
        crTime: null,
        upTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.provinceId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加热门省份";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const provinceId = row.provinceId || this.ids
      getProvince(provinceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改热门省份";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.provinceId != null) {
            updateProvince(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProvince(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const provinceIds = row.provinceId || this.ids;
      this.$modal.confirm('是否确认删除热门省份编号为"' + provinceIds + '"的数据项？').then(function() {
        return delProvince(provinceIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('store/province/export', {
        ...this.queryParams
      }, `province_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
